package cn.edu.scnu.thewonderofink.logic.network

import android.util.Log
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.WebSocket

object ServiceCreator {
    private const val BASE_URL = "ws://dav.emptylight.cn:26451"


    // 创建监听器
    private val myWebSocketListener = MyWebSocketListener()
    // 创建OkHttpClient实例
    private val client = OkHttpClient.Builder()
        .pingInterval(20, java.util.concurrent.TimeUnit.SECONDS)
        .connectTimeout(20, java.util.concurrent.TimeUnit.SECONDS)
        .retryOnConnectionFailure(true)
        .build()

    // 创建请求
    private val request = Request.Builder()
        .url(BASE_URL)
        .addHeader("RQ-From-Client", "MMQM")
        .addHeader("Result-Type", "SCORE")
        .build()

    private fun webSocketCreate(): WebSocket {
        return client.newWebSocket(request, myWebSocketListener)
    }
    fun create(): WebSocket {
        return webSocketCreate()
    }

    fun shutdown(): Unit = client.dispatcher.executorService.shutdown()

}